home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap10 / NoPopups / NoPopups.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.4 KB  |  111 lines

  1. /*-------------------------------------------------
  2.    NOPOPUPS.C -- Demonstrates No-Popup Nested Menu
  3.                  (c) Charles Petzold, 1998
  4.   -------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "resource.h"
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  12.                     PSTR szCmdLine, int iCmdShow)
  13. {
  14.      static TCHAR szAppName[] = TEXT ("NoPopUps") ;
  15.      HWND         hwnd ;
  16.      MSG          msg ;
  17.      WNDCLASS     wndclass ;
  18.      
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = NULL ;
  28.      wndclass.lpszClassName = szAppName ;
  29.      
  30.      if (!RegisterClass (&wndclass))
  31.      {
  32.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  33.                       szAppName, MB_ICONERROR) ;
  34.           return 0 ;
  35.      }
  36.      
  37.      hwnd = CreateWindow (szAppName, 
  38.                           TEXT ("No-Popup Nested Menu Demonstration"),
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.      
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.      
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.      {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.      }
  52.      return msg.wParam ;
  53. }
  54.  
  55. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  56. {
  57.      static HMENU hMenuMain, hMenuEdit, hMenuFile ;
  58.      HINSTANCE    hInstance ;
  59.      
  60.      switch (message)
  61.      {
  62.      case WM_CREATE:
  63.           hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE) ;
  64.           
  65.           hMenuMain = LoadMenu (hInstance, TEXT ("MenuMain")) ;
  66.           hMenuFile = LoadMenu (hInstance, TEXT ("MenuFile")) ;
  67.           hMenuEdit = LoadMenu (hInstance, TEXT ("MenuEdit")) ;
  68.           
  69.           SetMenu (hwnd, hMenuMain) ;
  70.           return 0 ;
  71.           
  72.      case WM_COMMAND:
  73.           switch (LOWORD (wParam))
  74.           {
  75.           case IDM_MAIN:
  76.                SetMenu (hwnd, hMenuMain) ;
  77.                return 0 ;
  78.                
  79.           case IDM_FILE:
  80.                SetMenu (hwnd, hMenuFile) ;
  81.                return 0 ;
  82.                
  83.           case IDM_EDIT:
  84.                SetMenu (hwnd, hMenuEdit) ;
  85.                return 0 ;
  86.                
  87.           case IDM_FILE_NEW:
  88.           case IDM_FILE_OPEN:
  89.           case IDM_FILE_SAVE:
  90.           case IDM_FILE_SAVE_AS:
  91.           case IDM_EDIT_UNDO:
  92.           case IDM_EDIT_CUT:
  93.           case IDM_EDIT_COPY:
  94.           case IDM_EDIT_PASTE:
  95.           case IDM_EDIT_CLEAR:
  96.                MessageBeep (0) ;
  97.                return 0 ;
  98.           }
  99.           break ;
  100.           
  101.      case WM_DESTROY:
  102.           SetMenu (hwnd, hMenuMain) ;
  103.           DestroyMenu (hMenuFile) ;
  104.           DestroyMenu (hMenuEdit) ;
  105.                
  106.           PostQuitMessage (0) ;
  107.           return 0 ;
  108.      }
  109.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  110. }
  111.